#include <ximera/xml/xmlelement.hpp>
#include <ximera/xml/xmltext.hpp>
#include <ximera/xml/xmliterator.hpp>

namespace ximera { namespace xml
{
    String const& XmlElement::attribute(String const& name)
    {
        return _attributes[name];
    }

    bool XmlElement::attribute(String const& name, String& value) const
    {
        AttributeConstIterator found = _attributes.find(name);
        if (found == _attributes.end())
            return false;

        value = found->second;
        return true;
    }

    void XmlElement::attribute(String const& name, String const& value)
    {
        _attributes[name] = value;
    }

    void XmlElement::removeAttribute(String const& name)
    {
        AttributeIterator found = _attributes.find(name);
        if (found == _attributes.end())
            return;

        removeAttribute(found);
    }

    void XmlElement::removeAttribute(AttributeIterator attr)
    {
        _attributes.erase(attr);
    }

    String XmlElement::retrieveText() const
    {
        String result;
        for (XmlIterator<XmlText> textNode(this); textNode; ++textNode)
            result += textNode->value();

        return result;
    }
}}